package com.xindaoapp.happypet.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.ExchangeResult;
import com.xindaoapp.happypet.bean.GoodsRecord;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * 我的兑换明细
 * 
 * 
 */
public class DuihuanDetailAdapter extends XinDaoBaseAdapter<GoodsRecord> {


    public DuihuanDetailAdapter(Context context, List<GoodsRecord> iniData, int pageSize, int res,
            int loadingRes) {
        super(context, iniData, pageSize, res, loadingRes);
        // TODO Auto-generated constructor stub
    }



    @Override
    public void nextPage(int start, int size, final ILoadNextPageData<GoodsRecord> iLoadNextPageData) {
        // TODO Auto-generated method stub
        super.nextPage(start, size, iLoadNextPageData);
        /**
         * 兑换商品记录
         */
        HappyPetApplication.get().getMoccaApi()
                .getGoodsRecord(start, size, new IRequest<List<GoodsRecord>>() {
                    @Override
                    public void request(List<GoodsRecord> t) {
                        iLoadNextPageData.loadNextPageData(t == null ? null : t);
                    }
                });

    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent, final GoodsRecord goods) {
        // TODO Auto-generated method stub
        ViewHolder holder = (ViewHolder) convertView.getTag();

        if (holder == null) {
            holder = new ViewHolder();
            // holder.tv_line = (TextView) convertView.findViewById(R.id.tv_line);
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_jifen = (TextView) convertView.findViewById(R.id.tv_jifen);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tv_status = (TextView) convertView.findViewById(R.id.tv_status);
            convertView.setTag(holder);
        }


        // if (position == 0) {
        // holder.tv_line.setVisibility(View.VISIBLE);
        // }


        ImageLoader.getInstance().displayImage(goods.goods_thumb, holder.icon);
        holder.tv_name.setText(goods.goods_name);
        holder.tv_jifen.setText(goods.exchange_integral + "尾巴币");
        holder.tv_time.setText(goods.updatetime);

        holder.tv_status.setText(goods.exchangetitle);


        convertView.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View arg0) {
                // TODO Auto-generated method stub
                if ("已取消".equals(goods.exchangetitle)) {
                    return false;
                }
                new AlertDialog.Builder(mContext).setTitle("温馨提示").setMessage("确定取消兑换本商品么 ?")
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();

                                HappyPetApplication
                                        .get()
                                        .getMoccaApi()
                                        .getQuxiaoExchangeResult(goods.integral_id,
                                                new IRequest<ExchangeResult>() {

                                                    @Override
                                                    public void request(ExchangeResult t) {
                                                        // TODO Auto-generated method stub
                                                        if (null != t) {
                                                            if (t.result.equals("0")) {
                                                                Toast.makeText(
                                                                        mContext.getApplicationContext(),
                                                                        t.msg, Toast.LENGTH_SHORT)
                                                                        .show();


                                                                CommonUtil.refurshWeiba(mContext);
                                                                // 刷新页面
                                                                // onLoadData();


                                                            } else {
                                                                Toast.makeText(
                                                                        mContext.getApplicationContext(),
                                                                        t.msg, Toast.LENGTH_SHORT)
                                                                        .show();
                                                            }
                                                        } else {

                                                            try {
                                                                Toast.makeText(
                                                                        mContext.getApplicationContext(),
                                                                        R.string.net_error,
                                                                        Toast.LENGTH_SHORT).show();
                                                            } catch (Exception e) {
                                                                e.printStackTrace();
                                                            }
                                                        }
                                                    }

                                                });
                            }
                        }).create().show();
                return false;
            }
        });



        return convertView;

    }

    class ViewHolder {
        ImageView icon;
        TextView tv_jifen, tv_status, tv_name, tv_time, tv_line;

    }

}
